package com.xhj.sportsnews.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.xhj.sportsnews.R;
import com.xhj.sportsnews.activity.Interest_ColumnActivity;
import com.xhj.sportsnews.adapter.News_ClassilyAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewsFragment extends Fragment implements View.OnClickListener, View.OnTouchListener {

    private ImageView imageadd;
    private List<String> titles;
    private TabLayout tabLayout;
//    private String[] stringTitle=new String[]{"推荐","电竞","视频","综合"};
    private News_ClassilyAdapter adapter;
    private List<Fragment> fragmentLists;
    private FragmentManager fm;
    private FragmentTransaction transantion;
    private ViewPager viewPager;
    public NewsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        String title= stringTitle.toString();


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View rootView=inflater.inflate(R.layout.fragment_news,container,false);
        initview(rootView);
        return rootView;
    }

    private void initview(View rootView) {
        imageadd = (ImageView) rootView.findViewById(R.id.news_image_add);
        imageadd.setOnClickListener(this);
        tabLayout= (TabLayout) rootView.findViewById(R.id.news_tabLayout);
        viewPager=(ViewPager)rootView.findViewById(R.id.news_viewpager);


        titles=new ArrayList<>();
        fragmentLists=new ArrayList<>();
        titles.add("推荐");
        titles.add("电竞");
        titles.add("网球");
        titles.add("羽毛球");
        titles.add("铿锵玫瑰");
        titles.add("竞猜");
        titles.add("图说");
        titles.add("高尔夫");
        titles.add("最“懂”球");
        titles.add("中国篮球");
        titles.add("中国足球");
        titles.add("国际足球");
        titles.add("综合");
        titles.add("视频");
        titles.add("NBA");
        titles.add("深度");
        fragmentLists=new ArrayList<>();

        News_RecommendFragment recommend=new News_RecommendFragment();
        News_GamingFragment gaming=new News_GamingFragment();
        fragmentLists.add(recommend);
        fragmentLists.add(gaming);
        fragmentLists.add(new News_TennisFragment());
        fragmentLists.add(new News_BadmintonFragment());
        fragmentLists.add(new News_FlowerFragment());
        fragmentLists.add(new News_GuessFragment());
        fragmentLists.add(new News_DrawingsFragment());
        fragmentLists.add(new News_GolfFragment());
        fragmentLists.add(new News_BestFragment());
        fragmentLists.add(new News_BasketballFragment());
        fragmentLists.add(new News_FootballFragment());
        fragmentLists.add(new News_NationalFootBall());
        fragmentLists.add(new News_AllFragment());
        fragmentLists.add(new News_VideoFragment());
        fragmentLists.add(new News_NBAFragment());
        fragmentLists.add(new News_DepthFragment());

//        fm=getActivity().getSupportFragmentManager();
//        transantion=fm.beginTransaction();
//        transantion.add(R.id.news_viewpager,recommend);
        adapter=new News_ClassilyAdapter(getActivity().getSupportFragmentManager(),fragmentLists,titles);
//        viewPager=new ViewPager(getContext());
        viewPager.setAdapter(adapter);
        viewPager.setOnTouchListener(this);
        tabLayout.setupWithViewPager(viewPager);
    }


    @Override
    public void onClick(View v) {
        Intent intent=new Intent(getContext(), Interest_ColumnActivity.class);
        startActivity(intent);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (event.getX() > 0) {
                    viewPager.requestDisallowInterceptTouchEvent(true);
                }
                break;
        }
        return false;

    }
}
